package com.xiaren.biz;

import javax.swing.JFrame;

import com.sun.media.sound.SoftTuning;
import com.xiaren.common.IConstant;
import com.xiaren.common.ObjectConstant;
import com.xiaren.ui.dialog.TitleWindow;
import com.xiaren.util.SystemTrayUtil;

import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 用于MainFrame的业务逻辑
 *
 * @author xiaren
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 */
public class FrameBiz {
	private static boolean isFirst = true;// 参数，判断是否是第一次出现绘图

	/**
	 * 设置JFrame的位置和大小
	 */
	public static void setBounds(JFrame frame,int x) {
		int localX = Integer.parseInt(IConstant.loyoutProperties.get(
				IConstant.LOCALX).toString());
		int localY = Integer.parseInt(IConstant.loyoutProperties.get(
				IConstant.LOCALY).toString());
		int width = Integer.parseInt(IConstant.loyoutProperties.get(
				IConstant.WIDTH).toString());
		int height = Integer.parseInt(IConstant.loyoutProperties.get(
				IConstant.HEIGHT).toString());
		frame.setBounds(localX, localY, width + x, height);

	}

	/**
	 * 设置MainFrame居中时的样子<br/>
	 * 暂时没用
	 */
	public static void setLocationCenter(JFrame frame) {
		int localX = ObjectConstant.screenWidth >> 2;
		int localY = ObjectConstant.screenHeight >> 2;
		frame.setBounds(localX, localY, ObjectConstant.screenWidth >> 1,
				ObjectConstant.screenHeight >> 1);
	}
	/**
	 * 初始化操作
	 */
	public void init() {
		initOther();
		addEvent();
	}
	/**
	 * 其它初始化操作
	 */
	private void initOther(){
		setBounds(ObjectConstant.mainFrame,0);
		ObjectConstant.mainPanel.init();

		ObjectConstant.screenHeight = ObjectConstant.mainFrame.getToolkit().getScreenSize().height;
		ObjectConstant.screenWidth = ObjectConstant.mainFrame.getToolkit().getScreenSize().width;





	}

	/**
	 * 为窗体加载时候加载事件
	 */
	private void addEvent() {
		//追加窗体的按钮事件
		ObjectConstant.btnSave.addActionListener(ListenerEvent.btnSave());
		ObjectConstant.btnSaveAll.addActionListener(ListenerEvent.btnSaveAll());
		ObjectConstant.btnExit.addActionListener(ListenerEvent.btnExit());
		ObjectConstant.btnHelp.addActionListener(ListenerEvent.btnHelp());
		ObjectConstant.btnVisible.addActionListener(ListenerEvent.btnVisible());
		ObjectConstant.btnIcon.addActionListener(ListenerEvent.btnIcon());
		ObjectConstant.btnRunCmd.addActionListener(ListenerEvent.btnRumCmd());
		ObjectConstant.menuPrint.addActionListener(ListenerEvent.menuItemPrint());


	}


	/**
	 * 使窗体可见
	 */
	public static void setFrameVisible(){
		ObjectConstant.mainFrame.setVisible(true);
		if (isFirst) {//此问题上尚未得到很好的解决方案
			setBounds(ObjectConstant.mainFrame,1);
			setBounds(ObjectConstant.mainFrame,-1);
			isFirst = false;
		}
	}




}
